'use strict';

const { Controller } = require('egg');
const { Op } = require('sequelize');
const serviceName = 'rabbitRoom'
class RouterController extends Controller {
    async allList() {
        const { service } = this.ctx;
        const { query } = this.ctx;
        const { farmId} = query;
        let where = {}
        if (farmId) {
            where.farm_id = farmId
        }
        const list = await service[serviceName].allList(where);
        this.ctx.success(list);
    }
    async sync() {
        const { service } = this.ctx;
        await service[serviceName].sync();
        this.ctx.success();
    }
    async staticData() {
        const { service } = this.ctx;
        let { roomId, startTime, endTime, code } = this.ctx.request.body;
        let where = {}
        if (roomId) {
            where.room = roomId
        }
        if (startTime && endTime) {
            where.date = {
                [Op.between]: [startTime, endTime]
              // 只需要取2点倍数得数据
            }
        }
        if (code &&code.length) {
            where.monitor_key = {
                [Op.in]: code
            }
        }
        const data = await service[serviceName].staticData(where, code, startTime, endTime);
        this.ctx.success(data);
    }

    async bodyWeight() {
        const { service } = this.ctx;
        const { body } = this.ctx.request;
        const data = await service[serviceName].bodyWeight(body);
        this.ctx.success(data);
    }
    async unhealthy() {
        const { service } = this.ctx;
        const { body } = this.ctx.request;
        const data = await service[serviceName].unhealthy(body);
        this.ctx.success(data);
    }
    async temEvaluate() {
        const { service } = this.ctx;
        const { body } = this.ctx.request;
        const data = await service[serviceName].temEvaluate(body);
        this.ctx.success(data);
    }
    async temThreshold() {
        const { service } = this.ctx;
        const { body } = this.ctx.request;
        const data = await service[serviceName].temThreshold(body);
        this.ctx.success(data);
    }
    async deviceInfo() {
        const { service } = this.ctx;
        const { body } = this.ctx.request;
        const data = await service[serviceName].deviceInfo(body);
        this.ctx.success(data);
    }
}

module.exports = RouterController;
